"""blockweb URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
import threading

import time

import datetime

import fcntl

import atexit
from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^v1/', include('blockrestful.urls')),
]


from apscheduler.schedulers.background import BackgroundScheduler
from blockrestful.views import fun_timer_block,fun_timer_txs

f = open("scheduler.lock", "wb")
try:
    fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
except:
    pass
else:
    scheduler = BackgroundScheduler()
    scheduler.add_job(fun_timer_block, 'interval', seconds=3,max_instances=10)  # 间隔3秒钟执行一次
    scheduler.add_job(fun_timer_txs, 'interval', seconds=3,max_instances=10)  # 间隔3秒钟执行一次
    scheduler.start()  # 这里的调度任务是独立的一个线程
def unlock():
    fcntl.flock(f, fcntl.LOCK_UN)
    f.close()

atexit.register(unlock)

# scheduler2 = BackgroundScheduler()
# scheduler2.add_job(fun_timer_txs, 'interval', seconds=3)#间隔3秒钟执行一次
# scheduler2.start()    #这里的调度任务是独立的一个线程

# scheduler2 = BackgroundScheduler()
# scheduler2.add_job(while_update, next_run_time=datetime.datetime.now() + datetime.timedelta(seconds=5))#间隔3秒钟执行一次
# scheduler2.start()    #这里的调度任务是独立的一个线程
# scheduler2.shutdown()

# t = threading.Thread(target=while_update)
# t.setDaemon(True)
# t.start()